package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiAccessoryButton;
import com.hilaver.dzmis.codetable.CtMaterialType;
import com.hilaver.dzmis.codetable.CtShape;
import com.hilaver.dzmis.codetable.CtSupplyType;
import com.hilaver.dzmis.dao.BiAccessoryButtonDAO;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class BiAccessoryButtonServiceImpl extends AbstractBaseService {

	public final static String COLUMN_MATERIAL_TYPE = "ctMaterialType";

	public final static String COLUMN_BRAND = "ctBrand";
	
	public final static String COLUMN_MEASURE = "measure";

	public final static String COLUMN_IS_PURCHASE = "isPurchase";

	public final static String COLUMN_REFERENCE = "reference";

	protected BiAccessoryButtonDAO dao;
	
	private CtServiceImpl ctService;

	public BiAccessoryButtonServiceImpl() {
		this.ctService = new CtServiceImpl();
		this.dao = new BiAccessoryButtonDAO();
	}

	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(BiAccessoryButton.class.getName(), id);
		return super.delete(BiAccessoryButton.class.getName(), id);
	}

	public String edit(BiAccessoryButton biAB) throws Exception {
		String operation = null;
		if (biAB.getId() != null) {
			BiAccessoryButton old = (BiAccessoryButton) this.dao.get(
					BiAccessoryButton.class.getName(), biAB.getId());
			if(biAB.getPhoto1Name() == null){
				biAB.setPhoto1(old.getPhoto1());
				biAB.setPhoto1Name(old.getPhoto1Name());
			}
			if(biAB.getPhoto2Name() == null){
				biAB.setPhoto2(old.getPhoto2());
				biAB.setPhoto2Name(old.getPhoto2Name());
			}
			if(biAB.getPhoto3Name() == null){
				biAB.setPhoto3(old.getPhoto3());
				biAB.setPhoto3Name(old.getPhoto3Name());
			}
			this.dao.getSession().evict(old);
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(biAB);
		super.editSysMbox(biAB, operation);
		return "{success: true}";
	}

	@Override
	public String get(int id) throws Exception {
		BiAccessoryButton biAB = (BiAccessoryButton) this.dao.get(
				BiAccessoryButton.class.getName(), id);

		writeFile(realFileDir + biAB.getPhoto1Name(), biAB.getPhoto1());
		writeFile(realFileDir + biAB.getPhoto2Name(), biAB.getPhoto2());
		writeFile(realFileDir + biAB.getPhoto3Name(), biAB.getPhoto3());

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);

		if(biAB.getBiFranceCustomer() != null){
			xmlSB.append("<" + COLUMN_CUSTOMER_ID + ">");
			xmlSB.append(biAB.getBiFranceCustomer().getId());
			xmlSB.append("</" + COLUMN_CUSTOMER_ID + ">");
			
			xmlSB.append("<customerName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAB.getBiFranceCustomer().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerName>");
			
			xmlSB.append("<customerDepartment>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAB.getBiFranceCustomer().getDepartment()==null?"":biAB.getBiFranceCustomer().getDepartment());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</customerDepartment>");
		}
		
		if(biAB.getBiFactory() != null){
			xmlSB.append("<" + COLUMN_FACTORY_ID + ">");
			xmlSB.append(biAB.getBiFactory().getId());
			xmlSB.append("</" + COLUMN_FACTORY_ID + ">");
			xmlSB.append("<" + COLUMN_FACTORY_NAME + "CN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAB.getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_FACTORY_NAME + "CN>");
			xmlSB.append("<" + COLUMN_FACTORY_NAME + "EN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAB.getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_FACTORY_NAME + "EN>");
		}
		
		xmlSB.append("<photo1Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biAB.getPhoto1Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biAB.getPhoto1Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo1Name>");
		
		xmlSB.append("<photo2Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biAB.getPhoto2Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biAB.getPhoto2Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo2Name>");
		
		xmlSB.append("<photo3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biAB.getPhoto3Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biAB.getPhoto3Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo3Name>");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(biAB));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String getWithDescription(int id) throws Exception{
		BiAccessoryButton biAB = (BiAccessoryButton) this.dao.get(
				BiAccessoryButton.class.getName(), id);
	
		writeFile(realFileDir + biAB.getPhoto1Name(), biAB.getPhoto1());
		writeFile(realFileDir + biAB.getPhoto2Name(), biAB.getPhoto2());
		writeFile(realFileDir + biAB.getPhoto3Name(), biAB.getPhoto3());
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append(this.toXMLFragment(biAB));
		xmlSB.append(SimpleObj2XML.toXMLFragment(biAB));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public String getAllPagination(int page, String sort, String order,
			String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(BiAccessoryButton.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLFragment((BiAccessoryButton) obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiAccessoryButton.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String toXMLFragment(BiAccessoryButton biAB) throws Exception {
		StringBuffer xmlSB = new StringBuffer();
		
		writeFile(realFileDir + biAB.getPhoto1Name(), biAB.getPhoto1());
		writeFile(realFileDir + biAB.getPhoto2Name(), biAB.getPhoto2());
		writeFile(realFileDir + biAB.getPhoto3Name(), biAB.getPhoto3());
		
		xmlSB.append("<" + COLUMN_MATERIAL_TYPE + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtMaterialType.class
				.getName(), biAB.getCtMaterialType(), this.locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_MATERIAL_TYPE + ">");
		
		xmlSB.append("<ctShape>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtShape.class
				.getName(), biAB.getCtShape(), this.locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctShape>");
		
		xmlSB.append("<ctSupplyTypeCode>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(biAB.getCtSupplyType());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctSupplyTypeCode>");

		xmlSB.append("<ctSupplyType>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtSupplyType.class.getName(),
				biAB.getCtSupplyType(), this.locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctSupplyType>");
		
		if(biAB.getBiFranceCustomer() != null){
			xmlSB.append("<" + COLUMN_CUSTOMER_NAME + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAB.getBiFranceCustomer().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_CUSTOMER_NAME + ">");
			xmlSB.append("<" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAB.getBiFranceCustomer().getDepartment()==null?"":biAB.getBiFranceCustomer().getDepartment());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
		}
		
		if(biAB.getBiFactory() != null){
			xmlSB.append("<" + COLUMN_FACTORY_NAME + "CN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAB.getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_FACTORY_NAME + "CN>");
			xmlSB.append("<" + COLUMN_FACTORY_NAME + "EN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAB.getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_FACTORY_NAME + "EN>");
			
			xmlSB.append("<addressFactoryCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAB.getBiFactory().getAddressFactoryCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryCN>");
			
			xmlSB.append("<addressFactoryEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(biAB.getBiFactory().getAddressFactoryEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryEN>");
		}

		xmlSB.append("<photo1Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biAB.getPhoto1Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biAB.getPhoto1Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo1Name>");
		
		xmlSB.append("<photo2Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biAB.getPhoto2Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biAB.getPhoto2Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo2Name>");
		
		xmlSB.append("<photo3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(biAB.getPhoto3Name() == null){
			xmlSB.append(NO_IMAGE_NAME);
		}else{
			xmlSB.append(biAB.getPhoto3Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</photo3Name>");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(biAB));

		return xmlSB.toString();
	}

	public String getMaxReferenceIndex(String ctMaterialType, String ctShape, Integer holeQuantity, Integer measure) throws Exception{
		List<Integer> referenceIndexList = this.dao.getMaxReferenceIndex(ctMaterialType, ctShape, holeQuantity, measure);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(referenceIndexList.size() == 0 || referenceIndexList.get(0) == null){
			xmlSB.append("0001");
		}else{
			Integer index = referenceIndexList.get(0);
			xmlSB.append(StringUtils.to4Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
}
